// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// Data type to represent a module import directive

#include <cwchar>
#include <cstring>
#include <new>
#include "util/String.h"
#include "fe/Import.h"

static std::wstring extractAlias(const std::wstring &clsName)
{
	const wchar_t *lastColon = wcsrchr(clsName.c_str(), ':');
	const wchar_t *alias = (lastColon) ? lastColon + 1 : clsName.c_str();
	return std::wstring(alias);
}

Import::Import(const std::wstring &importClsName, const std::wstring &alias, bool isStaticImport)
	: m_importClsName(importClsName)
	, m_alias(alias)
	, m_isStaticImport(isStaticImport)
{
}

Import::~Import()
{
}

Import *Import::alloc(const std::wstring &importClsName)
{
	return alloc(importClsName, extractAlias(importClsName));
}

Import *Import::alloc(const std::wstring &importClsName, const std::wstring &alias)
{
	return new Import(importClsName, alias);
}

Import *Import::allocStatic(const std::wstring &importModuleName, const std::wstring &methodName)
{
	return new Import(importModuleName, methodName, true);
}

void Import::destroy(Import *import)
{
	delete import;
}

const std::wstring &Import::getImportClsName() const
{
	return m_importClsName;
}

const std::wstring &Import::getAlias() const
{
	return m_alias;
}
